if (window.plus) {
    accountbindPageReady();
} else {
    document.addEventListener('plusready', accountbindPageReady, false);
}

$(function () {
    if (AGENT_CLIENT.WEB) {
        accountbindPageReady();
    }
})

var _currentloginuser;
function accountbindPageReady() {
    _currentloginuser = getLocalStorage(STORAGE_MANAGER.user);
    var accountbindPage = function () {
        this.init();
    };

    accountbindPage.prototype = {
        // 初始化
        init: function () {
            this.loadBindAccount();
            this.bindEvent();
        },
        // 用户绑定账号
        loadBindAccount: function () {
            var postdata = {};
            $.ajax({
                type: 'GET',
                url: CONFIG_MANAGER.RequstUrl + "v1/account/main/" + _currentloginuser.id
            }).done(function (res) {
                var typelist = res;
                // QQ
                if (typelist.indexOf(2) > -1) {
                    $("#aiqq").find("b[class=ybzt]").text("已绑定").css("color", "#6db324");
                    $("#aiqq").find("i").hide();
                }
                // 微信
                if (typelist.indexOf(1) > -1) {
                    $("#aiweixin").find("b[class=ybzt]").text("已绑定").css("color", "#6db324");
                    $("#aiweixin").find("i").hide();
                }
                // 新浪
                if (typelist.indexOf(3) > -1) {
                    $("#aisinaweibo").find("b[class=ybzt]").text("已绑定").css("color", "#6db324");
                    $("#aisinaweibo").find("i").hide();
                }

                $("p[name=accountitem]").on("click", function () {
                    if ($(this).text().indexOf("已绑定") > -1) {
                        return;
                    }

                    _thirdaccountPage.thirdLogin($(this).attr("tip"));
                });
            });
        },
        bindEvent: function () {
            $("#back").on("click", function () {
                back();
            });
        }
    };

    var _accountbindPage = new accountbindPage();

    var _thirdaccountPage = new thirdaccountPage();
    _thirdaccountPage.getUserInfo = function (userinfo) {
        // 未关联该账号，注册
        setPageParams(PAGE_NAME.account_bindaccount, {
            typeid: userinfo.typeid,
            mainid: _currentloginuser.id,
            openid: userinfo.openid,
            name: userinfo.name
        });

        console.log("userinfo:" + JSON.stringify(userinfo));
        redirect("../account/bindaccount.html");
    };
}
